﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing.plaf.basic {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.*;
    import org.aswing.geom.*;
    import org.aswing.plaf.*;
    import org.aswing.event.*;
    import org.aswing.graphics.*;
    import org.aswing.util.*;
    import org.aswing.plaf.basic.icon.*;
    import flash.ui.*;

    public class BasicScrollBarUI extends BaseComponentUI {

        private static var scrollSpeedThrottle:int = 60;
        private static var initialScrollSpeedThrottle:int = 500;

        protected var scrollBarWidth:int;
        protected var minimumThumbLength:int;
        protected var thumbRect:IntRectangle;
        protected var isDragging:Boolean;
        protected var offset:int;
        private var arrowShadowColor:ASColor;
        private var arrowLightColor:ASColor;
        protected var scrollbar:JScrollBar;
        protected var thumMC:AWSprite;
        protected var thumbDecorator:GroundDecorator;
        protected var incrButton:JButton;
        protected var decrButton:JButton;
        protected var leftIcon:Icon;
        protected var rightIcon:Icon;
        protected var upIcon:Icon;
        protected var downIcon:Icon;
        private var scrollTimer:Timer;
        private var scrollIncrement:int;
        private var scrollContinueDestination:int;

        public function BasicScrollBarUI(){
            this.scrollBarWidth = 16;
            this.minimumThumbLength = 9;
            this.thumbRect = new IntRectangle();
            this.isDragging = false;
            this.offset = 0;
            this.scrollIncrement = 0;
        }
        protected function getPropertyPrefix():String{
            return ("ScrollBar.");
        }
        override public function installUI(_arg1:Component):void{
            this.scrollbar = JScrollBar(_arg1);
            this.installDefaults();
            this.installComponents();
            this.installListeners();
        }
        override public function uninstallUI(_arg1:Component):void{
            this.scrollbar = JScrollBar(_arg1);
            this.uninstallDefaults();
            this.uninstallComponents();
            this.uninstallListeners();
        }
        protected function installDefaults():void{
            this.configureScrollBarColors();
            var _local1:String = this.getPropertyPrefix();
            if (containsKey((_local1 + "barWidth"))){
                this.scrollBarWidth = getInt((_local1 + "barWidth"));
            };
            if (containsKey((_local1 + "minimumThumbLength"))){
                this.minimumThumbLength = getInt((_local1 + "minimumThumbLength"));
            };
            LookAndFeel.installBasicProperties(this.scrollbar, _local1);
            LookAndFeel.installBorderAndBFDecorators(this.scrollbar, _local1);
        }
        private function configureScrollBarColors():void{
            var _local1:String = this.getPropertyPrefix();
            LookAndFeel.installColorsAndFont(this.scrollbar, _local1);
            this.arrowShadowColor = getColor((_local1 + "arrowShadowColor"));
            this.arrowLightColor = getColor((_local1 + "arrowLightColor"));
        }
        protected function uninstallDefaults():void{
            LookAndFeel.uninstallBorderAndBFDecorators(this.scrollbar);
        }
        protected function installComponents():void{
            this.thumMC = new AWSprite();
            var _local1:String = this.getPropertyPrefix();
            this.thumbDecorator = getGroundDecorator((_local1 + "thumbDecorator"));
            if (this.thumbDecorator != null){
                if (this.thumbDecorator.getDisplay(this.scrollbar) != null){
                    this.thumMC.addChild(this.thumbDecorator.getDisplay(this.scrollbar));
                };
            };
            this.scrollbar.addChild(this.thumMC);
            this.thumMC.addEventListener(MouseEvent.MOUSE_DOWN, this.__startDragThumb);
            this.thumMC.addEventListener(ReleaseEvent.RELEASE, this.__stopDragThumb);
            this.createIcons();
            this.incrButton = this.createArrowButton();
            this.incrButton.setName("JScrollbar_incrButton");
            this.decrButton = this.createArrowButton();
            this.decrButton.setName("JScrollbar_decrButton");
            this.setButtonIcons();
            this.incrButton.setUIElement(true);
            this.decrButton.setUIElement(true);
            this.scrollbar.addChild(this.incrButton);
            this.scrollbar.addChild(this.decrButton);
            this.scrollbar.setEnabled(this.scrollbar.isEnabled());
        }
        protected function uninstallComponents():void{
            this.scrollbar.removeChild(this.incrButton);
            this.scrollbar.removeChild(this.decrButton);
            this.scrollbar.removeChild(this.thumMC);
            this.thumMC.removeEventListener(MouseEvent.MOUSE_DOWN, this.__startDragThumb);
            this.thumMC.removeEventListener(ReleaseEvent.RELEASE, this.__stopDragThumb);
            this.thumbDecorator = null;
        }
        protected function installListeners():void{
            this.scrollbar.addStateListener(this.__adjustChanged);
            this.incrButton.addEventListener(MouseEvent.MOUSE_DOWN, this.__incrButtonPress);
            this.incrButton.addEventListener(ReleaseEvent.RELEASE, this.__incrButtonReleased);
            this.decrButton.addEventListener(MouseEvent.MOUSE_DOWN, this.__decrButtonPress);
            this.decrButton.addEventListener(ReleaseEvent.RELEASE, this.__decrButtonReleased);
            this.scrollbar.addEventListener(MouseEvent.MOUSE_DOWN, this.__trackPress);
            this.scrollbar.addEventListener(ReleaseEvent.RELEASE, this.__trackReleased);
            this.scrollbar.addEventListener(MouseEvent.MOUSE_WHEEL, this.__onMouseWheel);
            this.scrollbar.addEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, this.__onKeyDown);
            this.scrollbar.addEventListener(Event.REMOVED_FROM_STAGE, this.__destroy);
            this.scrollTimer = new Timer(scrollSpeedThrottle);
            this.scrollTimer.setInitialDelay(initialScrollSpeedThrottle);
            this.scrollTimer.addActionListener(this.__scrollTimerPerformed);
        }
        protected function uninstallListeners():void{
            this.scrollbar.removeStateListener(this.__adjustChanged);
            this.incrButton.removeEventListener(MouseEvent.MOUSE_DOWN, this.__incrButtonPress);
            this.incrButton.removeEventListener(ReleaseEvent.RELEASE, this.__incrButtonReleased);
            this.decrButton.removeEventListener(MouseEvent.MOUSE_DOWN, this.__decrButtonPress);
            this.decrButton.removeEventListener(ReleaseEvent.RELEASE, this.__decrButtonReleased);
            this.scrollbar.removeEventListener(MouseEvent.MOUSE_DOWN, this.__trackPress);
            this.scrollbar.removeEventListener(ReleaseEvent.RELEASE, this.__trackReleased);
            this.scrollbar.removeEventListener(MouseEvent.MOUSE_WHEEL, this.__onMouseWheel);
            this.scrollbar.removeEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, this.__onKeyDown);
            this.scrollbar.removeEventListener(Event.REMOVED_FROM_STAGE, this.__destroy);
            this.scrollTimer.stop();
            this.scrollTimer = null;
        }
        protected function isVertical():Boolean{
            return ((this.scrollbar.getOrientation() == JScrollBar.VERTICAL));
        }
        protected function getThumbRect():IntRectangle{
            return (this.thumbRect.clone());
        }
        private function __destroy(_arg1:Event):void{
            this.scrollTimer.stop();
            if (this.isDragging){
                this.scrollbar.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMoveThumb);
            };
        }
        private function __onMouseWheel(_arg1:MouseEvent):void{
            if (!this.scrollbar.isEnabled()){
                return;
            };
            this.scrollByIncrement((-(_arg1.delta) * this.scrollbar.getUnitIncrement()));
        }
        private function __onKeyDown(_arg1:FocusKeyEvent):void{
            if (!((this.scrollbar.isEnabled()) && (this.scrollbar.isShowing()))){
                return;
            };
            var _local2:uint = _arg1.keyCode;
            if ((((_local2 == Keyboard.UP)) || ((_local2 == Keyboard.LEFT)))){
                this.scrollByIncrement(-(this.scrollbar.getUnitIncrement()));
            } else {
                if ((((_local2 == Keyboard.DOWN)) || ((_local2 == Keyboard.RIGHT)))){
                    this.scrollByIncrement(this.scrollbar.getUnitIncrement());
                } else {
                    if (_local2 == Keyboard.PAGE_UP){
                        this.scrollByIncrement(-(this.scrollbar.getBlockIncrement()));
                    } else {
                        if (_local2 == Keyboard.PAGE_DOWN){
                            this.scrollByIncrement(this.scrollbar.getBlockIncrement());
                        } else {
                            if (_local2 == Keyboard.HOME){
                                this.scrollbar.setValue(this.scrollbar.getMinimum());
                            } else {
                                if (_local2 == Keyboard.END){
                                    this.scrollbar.setValue((this.scrollbar.getMaximum() - this.scrollbar.getVisibleAmount()));
                                };
                            };
                        };
                    };
                };
            };
        }
        private function __scrollTimerPerformed(_arg1:AWEvent):void{
            var _local2:int = (this.scrollbar.getValue() + this.scrollIncrement);
            var _local3:Boolean;
            if (this.scrollIncrement > 0){
                if (_local2 >= this.scrollContinueDestination){
                    _local3 = true;
                };
            } else {
                if (_local2 <= this.scrollContinueDestination){
                    _local3 = true;
                };
            };
            if (_local3){
                this.scrollbar.setValue(this.scrollContinueDestination, false);
                this.scrollTimer.stop();
            } else {
                this.scrollByIncrement(this.scrollIncrement);
            };
        }
        private function __adjustChanged(_arg1:Event):void{
            if (((this.scrollbar.isVisible()) && (!(this.isDragging)))){
                this.paintAndLocateThumb(this.scrollbar.getPaintBounds());
            };
        }
        private function __incrButtonPress(_arg1:Event):void{
            this.scrollIncrement = this.scrollbar.getUnitIncrement();
            this.scrollByIncrement(this.scrollIncrement);
            this.scrollContinueDestination = (this.scrollbar.getMaximum() - this.scrollbar.getVisibleAmount());
            this.scrollTimer.restart();
        }
        private function __incrButtonReleased(_arg1:Event):void{
            this.scrollTimer.stop();
        }
        private function __decrButtonPress(_arg1:Event):void{
            this.scrollIncrement = -(this.scrollbar.getUnitIncrement());
            this.scrollByIncrement(this.scrollIncrement);
            this.scrollContinueDestination = this.scrollbar.getMinimum();
            this.scrollTimer.restart();
        }
        private function __decrButtonReleased(_arg1:Event):void{
            this.scrollTimer.stop();
        }
        private function __trackPress(_arg1:MouseEvent):void{
            var _local5:int;
            var _local2:IntPoint = this.scrollbar.getMousePosition();
            var _local3:Boolean;
            var _local4:IntRectangle = this.getThumbRect();
            if (this.isVertical()){
                _local5 = _local2.y;
                _local2.y = (_local2.y - (_local4.height / 2));
                if ((((_local5 < _local4.y)) && ((_local5 > (this.decrButton.y + this.decrButton.height))))){
                    _local3 = true;
                } else {
                    if ((((_local5 > (_local4.y + _local4.height))) && ((_local5 < this.incrButton.y)))){
                        _local3 = true;
                    };
                };
            } else {
                _local5 = _local2.x;
                _local2.x = (_local2.x - (_local4.width / 2));
                if ((((_local5 < _local4.x)) && ((_local5 > (this.decrButton.x + this.decrButton.width))))){
                    _local3 = true;
                } else {
                    if ((((_local5 > (_local4.x + _local4.width))) && ((_local5 < this.incrButton.x)))){
                        _local3 = true;
                    };
                };
            };
            if (_local3){
                this.scrollContinueDestination = this.getValueWithPosition(_local2);
                if (this.scrollContinueDestination > this.scrollbar.getValue()){
                    this.scrollIncrement = this.scrollbar.getBlockIncrement();
                } else {
                    this.scrollIncrement = -(this.scrollbar.getBlockIncrement());
                };
                this.scrollByIncrement(this.scrollIncrement);
                this.scrollTimer.restart();
            };
        }
        private function __trackReleased(_arg1:Event):void{
            this.scrollTimer.stop();
        }
        private function scrollByIncrement(_arg1:int):void{
            this.scrollbar.setValue((this.scrollbar.getValue() + _arg1), false);
        }
        private function __startDragThumb(_arg1:Event):void{
            if (!this.scrollbar.isEnabled()){
                return;
            };
            this.scrollbar.setValueIsAdjusting(true);
            var _local2:IntPoint = this.scrollbar.getMousePosition();
            var _local3:int = _local2.x;
            var _local4:int = _local2.y;
            var _local5:IntRectangle = this.getThumbRect();
            if (this.isVertical()){
                this.offset = (_local4 - _local5.y);
            } else {
                this.offset = (_local3 - _local5.x);
            };
            this.isDragging = true;
            this.__startHandleDrag();
        }
        private function __stopDragThumb(_arg1:Event):void{
            this.__stopHandleDrag();
            if (!this.scrollbar.isEnabled()){
                return;
            };
            if (this.isDragging){
                this.scrollThumbToCurrentMousePosition();
            };
            this.offset = 0;
            this.isDragging = false;
            this.scrollbar.setValueIsAdjusting(false);
        }
        private function __startHandleDrag():void{
            this.scrollbar.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.__onMoveThumb, false, 0, true);
        }
        private function __stopHandleDrag():void{
            this.scrollbar.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMoveThumb);
        }
        private function __onMoveThumb(_arg1:MouseEvent):void{
            if (!this.scrollbar.isEnabled()){
                return;
            };
            this.scrollThumbToCurrentMousePosition();
            _arg1.updateAfterEvent();
        }
        private function scrollThumbToCurrentMousePosition():void{
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local1:IntPoint = this.scrollbar.getMousePosition();
            var _local2:int = _local1.x;
            var _local3:int = _local1.y;
            var _local4:IntRectangle = this.getThumbRect();
            if (this.isVertical()){
                _local5 = (this.decrButton.getY() + this.decrButton.getHeight());
                _local6 = (this.incrButton.getY() - _local4.height);
                _local7 = Math.min(_local6, Math.max(_local5, (_local3 - this.offset)));
                this.setThumbRect(_local4.x, _local7, _local4.width, _local4.height);
            } else {
                _local5 = (this.decrButton.getX() + this.decrButton.getWidth());
                _local6 = (this.incrButton.getX() - _local4.width);
                _local7 = Math.min(_local6, Math.max(_local5, (_local2 - this.offset)));
                this.setThumbRect(_local7, _local4.y, _local4.width, _local4.height);
            };
            var _local8:int = this.getValueWithThumbMaxMinPos(_local5, _local6, _local7);
            this.scrollbar.setValue(_local8, false);
        }
        private function getValueWithPosition(_arg1:IntPoint):int{
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local2:int = _arg1.x;
            var _local3:int = _arg1.y;
            var _local4:IntRectangle = this.getThumbRect();
            if (this.isVertical()){
                _local5 = (this.decrButton.getY() + this.decrButton.getHeight());
                _local6 = (this.incrButton.getY() - _local4.height);
                _local7 = _local3;
            } else {
                _local5 = (this.decrButton.getX() + this.decrButton.getWidth());
                _local6 = (this.incrButton.getX() - _local4.width);
                _local7 = _local2;
            };
            return (this.getValueWithThumbMaxMinPos(_local5, _local6, _local7));
        }
        private function getValueWithThumbMaxMinPos(_arg1:int, _arg2:int, _arg3:int):int{
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local9:int;
            var _local10:int;
            var _local4:BoundedRangeModel = this.scrollbar.getModel();
            if (_arg3 >= _arg2){
                _local5 = (_local4.getMaximum() - _local4.getExtent());
            } else {
                _local6 = (_local4.getMaximum() - _local4.getExtent());
                _local7 = (_local6 - _local4.getMinimum());
                _local8 = (_arg3 - _arg1);
                _local9 = (_arg2 - _arg1);
                _local10 = Math.round(((_local8 / _local9) * _local7));
                _local5 = (_local10 + _local4.getMinimum());
            };
            return (_local5);
        }
        override public function paint(_arg1:Component, _arg2:Graphics2D, _arg3:IntRectangle):void{
            super.paint(_arg1, _arg2, _arg3);
            this.layoutScrollBar();
            this.paintAndLocateThumb(_arg3);
        }
        override protected function paintBackGround(_arg1:Component, _arg2:Graphics2D, _arg3:IntRectangle):void{
        }
        private function paintAndLocateThumb(_arg1:IntRectangle):void{
            var _local6:int;
            var _local7:int;
            var _local9:int;
            if (!this.scrollbar.isEnabled()){
                if (this.isVertical()){
                    if (this.scrollbar.mouseChildren){
                        trace("Logic Wrong : Scrollbar is not enabled, but its children enabled ");
                    };
                };
                this.thumMC.visible = false;
                return;
            };
            this.thumMC.visible = true;
            var _local2:int = this.scrollbar.getMinimum();
            var _local3:int = this.scrollbar.getVisibleAmount();
            var _local4:int = (this.scrollbar.getMaximum() - _local2);
            var _local5:int = this.scrollbar.getValue();
            if (_local4 <= 0){
                if (_local4 < 0){
                    trace(((((("Logic Wrong : Scrollbar range = " + _local4) + ", max=") + this.scrollbar.getMaximum()) + ", min=") + _local2));
                };
                this.thumMC.visible = false;
                return;
            };
            if (this.isVertical()){
                _local6 = ((_arg1.height - this.incrButton.getHeight()) - this.decrButton.getHeight());
                _local7 = Math.floor((_local6 * (_local3 / _local4)));
            } else {
                _local6 = ((_arg1.width - this.incrButton.getWidth()) - this.decrButton.getWidth());
                _local7 = Math.floor((_local6 * (_local3 / _local4)));
            };
            if (_local6 > this.minimumThumbLength){
                _local7 = Math.max(_local7, this.minimumThumbLength);
            } else {
                this.thumMC.visible = false;
                return;
            };
            var _local8:int = (_local6 - _local7);
            if ((_local4 - _local3) == 0){
                _local9 = 0;
            } else {
                _local9 = Math.round((_local8 * ((_local5 - _local2) / (_local4 - _local3))));
            };
            if (this.isVertical()){
                this.setThumbRect(_arg1.x, ((_local9 + _arg1.y) + this.decrButton.getHeight()), this.scrollBarWidth, _local7);
            } else {
                this.setThumbRect(((_local9 + _arg1.x) + this.decrButton.getWidth()), _arg1.y, _local7, this.scrollBarWidth);
            };
        }
        private function setThumbRect(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
            var _local5:int = this.thumbRect.width;
            var _local6:int = this.thumbRect.height;
            this.thumbRect.setRectXYWH(_arg1, _arg2, _arg3, _arg4);
            if (((!((_arg3 == _local5))) || (!((_arg4 == _local6))))){
                this.paintThumb(this.thumMC, this.thumbRect.getSize(), this.isDragging);
            };
            this.thumMC.x = _arg1;
            this.thumMC.y = _arg2;
        }
        private function paintThumb(_arg1:Sprite, _arg2:IntDimension, _arg3:Boolean):void{
            _arg1.graphics.clear();
            var _local4:Graphics2D = new Graphics2D(_arg1.graphics);
            if (this.thumbDecorator != null){
                this.thumbDecorator.updateDecorator(this.scrollbar, _local4, _arg2.getBounds());
            };
        }
        protected function createArrowIcon(_arg1:Number):Icon{
            var _local2:Icon = new ScrollBarArrowIcon(_arg1, this.scrollBarWidth, this.scrollBarWidth);
            return (_local2);
        }
        protected function createArrowButton():JButton{
            var _local1:JButton = new JButton();
            _local1.setFocusable(false);
            _local1.setBackground(null);
            _local1.setForeground(null);
            _local1.setMideground(null);
            _local1.setStyleTune(null);
            _local1.setStyleProxy(this.scrollbar);
            _local1.setPreferredSize(new IntDimension(this.scrollBarWidth, this.scrollBarWidth));
            return (_local1);
        }
        protected function createIcons():void{
            this.leftIcon = this.createArrowIcon(Math.PI);
            this.rightIcon = this.createArrowIcon(0);
            this.upIcon = this.createArrowIcon((-(Math.PI) / 2));
            this.downIcon = this.createArrowIcon((Math.PI / 2));
        }
        protected function setButtonIcons():void{
            if (this.isVertical()){
                this.incrButton.setIcon(this.downIcon);
                this.decrButton.setIcon(this.upIcon);
            } else {
                this.incrButton.setIcon(this.rightIcon);
                this.decrButton.setIcon(this.leftIcon);
            };
        }
        override public function getPreferredSize(_arg1:Component):IntDimension{
            var _local2:int;
            var _local3:int;
            if (this.isVertical()){
                _local2 = this.scrollBarWidth;
                _local3 = (this.scrollBarWidth * 2);
            } else {
                _local2 = (this.scrollBarWidth * 2);
                _local3 = this.scrollBarWidth;
            };
            return (this.scrollbar.getInsets().getOutsideSize(new IntDimension(_local2, _local3)));
        }
        override public function getMaximumSize(_arg1:Component):IntDimension{
            var _local2:int;
            var _local3:int;
            if (this.isVertical()){
                _local2 = this.scrollBarWidth;
                _local3 = 100000;
            } else {
                _local2 = 100000;
                _local3 = this.scrollBarWidth;
            };
            return (this.scrollbar.getInsets().getOutsideSize(new IntDimension(_local2, _local3)));
        }
        override public function getMinimumSize(_arg1:Component):IntDimension{
            return (this.getPreferredSize(_arg1));
        }
        protected function layoutVScrollbar(_arg1:JScrollBar):void{
            var _local2:IntRectangle = _arg1.getPaintBounds();
            var _local3:IntDimension = this.decrButton.getPreferredSize();
            var _local4:int = _local3.width;
            var _local5:int = _local3.height;
            var _local6:int = _local2.x;
            var _local7:int = _local2.y;
            var _local8:int = this.scrollBarWidth;
            this.decrButton.setComBoundsXYWH((_local6 + ((_local8 - _local4) / 2)), _local7, _local4, _local5);
            this.incrButton.setComBoundsXYWH((_local6 + ((_local8 - _local4) / 2)), ((_local7 + _local2.height) - _local5), _local4, _local5);
        }
        protected function layoutHScrollbar(_arg1:JScrollBar):void{
            var _local2:IntRectangle = _arg1.getPaintBounds();
            var _local3:IntDimension = this.decrButton.getPreferredSize();
            var _local4:int = _local3.width;
            var _local5:int = _local3.height;
            var _local6:int = _local2.x;
            var _local7:int = _local2.y;
            var _local8:int = this.scrollBarWidth;
            this.decrButton.setComBoundsXYWH(_local6, (_local7 + ((_local8 - _local5) / 2)), _local4, _local5);
            this.incrButton.setComBoundsXYWH(((_local6 + _local2.width) - _local4), (_local7 + ((_local8 - _local5) / 2)), _local4, _local5);
        }
        public function layoutScrollBar():void{
            if (this.isDragging){
                return;
            };
            this.setButtonIcons();
            if (this.isVertical()){
                this.layoutVScrollbar(this.scrollbar);
            } else {
                this.layoutHScrollbar(this.scrollbar);
            };
        }

    }
}//package org.aswing.plaf.basic 
